package org.mybatis.generator.codegen.spring.jdbc.repository.elements;

import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.codegen.spring.jdbc.repository.AbstractRepGenerator;

public class DeleteByPrimaryKeyMethodGenerator extends AbstractRepGenerator {

	@Override
	public void addImplementationElements(TopLevelClass topLevelClass) {

		// 添加依赖类
		addImportedType(topLevelClass);

		// 设置方法
		Method method = new Method();
		method.addException(new FullyQualifiedJavaType("org.springframework.dao.DataAccessException"));
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName("deleteById");
		// 获得主键信息
		List<IntrospectedColumn> pks = introspectedTable.getPrimaryKeyColumns();

		// 方法内部代码
		StringBuilder sb = new StringBuilder();

		method.addBodyLine("Map<String, Object> params = new HashMap<String, Object>();");
		for (IntrospectedColumn introspectedColumn : pks) {
			String column = toCaseName(introspectedColumn.getActualColumnName());
			method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), column));
			StringBuffer sb_ = new StringBuffer("params.put(\"");
			sb_.append(column);
			sb_.append("\",");
			sb_.append(column);
			sb_.append(");");
			method.addBodyLine(sb_.toString());
		}
		sb.setLength(0);
		sb.append("jdbcTemplate.update(\"");
		sb.append(getSqlDelete(introspectedTable));
		sb.append(" WHERE ");

		for (int i = 0; i < pks.size(); i++) {
			IntrospectedColumn introspectedColumn = pks.get(i);
			if (i != 0) {
				sb.append(" and ");
			}
			String column = toCaseName(introspectedColumn.getActualColumnName());
			sb.append(column);
			sb.append(" = :");
			sb.append(column);
		}

		sb.append("\",params);");
		method.addBodyLine(sb.toString());

		context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);

		if (context.getPlugins().clientDeleteByPrimaryKeyMethodGenerated(method, topLevelClass, introspectedTable)) {
			topLevelClass.addMethod(method);
			addDeleteAllMethodElements(topLevelClass);
		}

	}

	/**
	 * @param topLevelClass
	 */
	private void addImportedType(TopLevelClass topLevelClass) {
		topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.Map"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.HashMap"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.springframework.dao.DataAccessException"));
	}

	private void addDeleteAllMethodElements(TopLevelClass topLevelClass) {

		Method method = new Method();
		method.addException(new FullyQualifiedJavaType("org.springframework.dao.DataAccessException"));
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName("deleteAll");
		// 方法内部代码
		StringBuilder sb = new StringBuilder();

		sb.setLength(0);
		sb.append("jdbcTemplate.getJdbcOperations().execute(\"");
		sb.append(getSqlDelete(introspectedTable));
		sb.append("\");");
		method.addBodyLine(sb.toString());

		context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);
	}

	private String getSqlDelete(IntrospectedTable introspectedTable) {
		StringBuilder sb = new StringBuilder("DELETE FROM ");
		sb.append(introspectedTable.getFullyQualifiedTable().getIntrospectedTableName());
		return sb.toString();
	}

}
